package com.linkstec.bookshop.controller;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.linkstec.bookshop.pojo.Book;
import com.linkstec.bookshop.pojo.Category;
import com.linkstec.bookshop.pojo.Goods;
import com.linkstec.bookshop.pojo.Order;
import com.linkstec.bookshop.pojo.User;
import com.linkstec.bookshop.service.BookService;
import com.linkstec.bookshop.service.CategoryService;
import com.linkstec.bookshop.service.CommentService;
import com.linkstec.bookshop.service.GoodsService;
import com.linkstec.bookshop.service.OrderService;
import com.linkstec.bookshop.service.UserService;
import com.linkstec.bookshop.util.PageBean;
import com.linkstec.bookshop.util.UUIDUtiils;
import com.linkstec.bookshop.vo.BookVo;
import com.linkstec.bookshop.vo.CommentVo;
import com.linkstec.bookshop.vo.GoodsVo;

/**   
 * @ClassName:  AdminController  管理员的controller 
 * @Description:TODO   
 * @author: yanyuan
 * @date:   2018年11月24日 下午2:49:42       
 */
@Controller
@RequestMapping("admin")
public class AdminController {
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private BookService bookService;
	
	@Autowired
	private GoodsService goodsService;
	
	@Autowired
	private CategoryService categroyService;
	
	@Autowired
	private CommentService commentService;
	
//----------------------------------------------------User
	/**     
	 * @Description:TODO 查找所有user
	 * @author: yanyuan
	 * @date:   2018年11月26日 上午10:23:48    
	 *  @param page
	 *  @return    
	 */ 
	@RequestMapping(value = "findAllUser")

	public ModelAndView findAllUser(int page) {
		ModelAndView mv = new ModelAndView();
		PageBean<User> userPageBean = new PageBean<User>();
			try {
				userPageBean =userService.findAllUser(page);
			} catch (Exception e) {
				e.printStackTrace();
			}
		mv.addObject("userPageBean", userPageBean);
		mv.setViewName("admin/user/list");
		return mv;
	}
	
	/**     
	 * @Description:TODO 禁用用户
	 * @author: yanyuan
	 * @date:   2018年11月26日 上午10:24:30    
	 *  @param userId
	 *  @param page
	 *  @return    
	 */ 
	@RequestMapping(value="userOffline")
	public String userOffline(int userId,int page) {
		try {
			userService.userOffline(userId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "redirect:/admin/findAllUser?page="+page;
	}
	
	/**     
	 * @Description:TODO 启用用户
	 * @author: yanyuan
	 * @date:   2018年11月26日 上午10:24:51    
	 *  @param userId
	 *  @param page
	 *  @return    
	 */ 
	@RequestMapping(value="userOnline")
	public String userOnline(int userId,int page) {
		try {
			userService.userOnline(userId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "redirect:/admin/findAllUser?page="+page;
	}
	
//----------------------------------------------------Book
	/**     
	 * @Description:TODO 查找所有Book
	 * @author: yanyuan
	 * @date:   2018年11月26日 上午10:27:06    
	 *  @param page
	 *  @return    
	 */ 
	@RequestMapping(value = "findAllBook")
	public ModelAndView findAllBookVo(int page) {
		ModelAndView mv = new ModelAndView();
		PageBean<BookVo> bookVoPageBean = new PageBean<BookVo>();
			try {
				bookVoPageBean = bookService.findAllBook(page);
			} catch (Exception e) {
				e.printStackTrace();
			}
		mv.addObject("bookVoPageBean", bookVoPageBean);
		mv.setViewName("admin/book/list");
		return mv;
	}
	
	/**     
	 * @Description:TODO 跳转添加书库
	 * @author: yanyuan
	 * @date:   2018年11月26日 上午10:36:10    
	 *  @return    
	 */ 
	@RequestMapping(value = "toAddBook")
	public ModelAndView toAddBook() {
		ModelAndView mv = new ModelAndView();
		List<Category> categorys = null;
		try {
			categorys = categroyService.findCategory();
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("categorys", categorys);
		mv.setViewName( "admin/book/add");
		return mv;
	}
	
	/**     
	 * @Description:TODO 添加书库
	 * @author: yanyuan
	 * @date:   2018年11月26日 上午11:24:12    
	 *  @param book
	 *  @return    
	 */ 
	@RequestMapping(value = "addBook")
	public String addBook(Book book,HttpServletRequest request,MultipartFile file) {
		try {
			if (file != null) {
				String path = request.getServletContext().getRealPath(
						"/products");
				String uploadFileName = file.getOriginalFilename();
				String fileName = UUIDUtiils.getUUID()+uploadFileName;
				File diskFile = new File(path + "//" + fileName);
				file.transferTo(diskFile);
				book.setImage("products/" + fileName);
			}
			System.out.println(book.getImage());
			bookService.addBook(book);
		} catch (Exception e) {
			// TODO: handle exception
			System.err.println(e);
		}
		return "redirect:/admin/findAllBook?page=1";
	}
	
	/**     
	 * @Description:TODO 修改book
	 * @author: yanyuan
	 * @date:   2018年11月26日 上午11:27:24    
	 *  @param bookId
	 *  @return    
	 */ 
	@RequestMapping(value = "toChangeBook")
	public ModelAndView toChangeBook(int bookId) {
		ModelAndView mv = new ModelAndView();
		Book book = null;
		List<Category> categorys = null;
		try {
			book = bookService.findBookById(bookId);
			categorys = categroyService.findCategory();
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("book", book);
		mv.addObject("categorys", categorys);
		mv.setViewName( "admin/book/edit");
		return mv;
	}
	
	@RequestMapping(value = "changeBook")
	public String changeBook(Book book,HttpServletRequest request,MultipartFile file) {
		try {
			if (file != null) {
				String path = request.getServletContext().getRealPath(
						"/products");
				//String path =new File("").getAbsolutePath();
				String uploadFileName = file.getOriginalFilename();
				String fileName = UUIDUtiils.getUUID()+uploadFileName;
				File diskFile = new File(path + "//" + fileName);
				file.transferTo(diskFile);
				book.setImage("products/" + fileName);
			}
			bookService.update(book);
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println(e);
		}
		return"redirect:/admin/findAllBook?page=1";
	}
	
//------------------------------------------------------------Goods
	
	@RequestMapping(value = "findAllGoods")
	public ModelAndView findAllGoods(int page) {
		ModelAndView mv = new ModelAndView();
		PageBean<GoodsVo> pageBean = new PageBean<GoodsVo>();
			try {
				pageBean = goodsService.findAllGoods(page);
			} catch (Exception e) {
				e.printStackTrace();
			}
		mv.addObject("pageBean", pageBean);
		mv.setViewName("admin/goods/list");
		return mv;
	}
	@RequestMapping(value = "toEditGoods")
	public ModelAndView toEditGoods(int goodsId) {
		ModelAndView mv = new ModelAndView();
		GoodsVo goodsVo = null;
		try {
			goodsVo = goodsService.findOneGoodsDetailById(goodsId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("goods", goodsVo);
		mv.setViewName( "admin/goods/edit");
		return mv;
	}
	@RequestMapping(value = "editGoods")
	public String editGoods(Goods goods,int addNum) {
		try {
			goodsService.update(goods,addNum);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return"redirect:/admin/findAllGoods?page=1";
	}
	@RequestMapping(value="goodsOffline")
	public String goodsOffline(int goodsId, int page) {
		
		try {
			goodsService.goodsOffline(goodsId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/admin/findAllGoods?page="+page;
	}
	@RequestMapping(value="goodsOnline")
	public String goodsOnline(int goodsId, int page) {
		
		try {
			goodsService.goodsOnline(goodsId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/admin/findAllGoods?page="+page;
	}
	
//***********************************************************Comment
	@RequestMapping(value = "findAllComment")
	public ModelAndView findAllComment(int page) {
		ModelAndView mv = new ModelAndView();
		PageBean<CommentVo> pageBean = new PageBean<CommentVo>();
			try {
				pageBean = commentService.findAllComment(page);
			} catch (Exception e) {
				e.printStackTrace();
			}
		mv.addObject("pageBean", pageBean);
		mv.setViewName("admin/message/list");
		return mv;
	}
	
	@RequestMapping(value = "deleteComment")
	public String deleteComment(int cmId,int page) {
		try {
			commentService.delete(cmId);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return"redirect:/admin/findAllComment?page="+page;
	}
	
//#################################################################  Order
	
	@RequestMapping(value = "findAllOrder")
	public ModelAndView findAllNewOrder(int page, int oState){
		ModelAndView mv = new ModelAndView();
		PageBean<Order> pageBean = new PageBean<Order>();
			try {
				pageBean = orderService.findAllOrder(page, oState);
			} catch (Exception e) {
				e.printStackTrace();
			}
		mv.addObject("pageBean", pageBean);
		mv.addObject("OState", oState);
		mv.setViewName("admin/order/list");
		return mv;
	}
	
	@RequestMapping(value = "delivery")
	public String delivery(int oId, int page) {
		try {
			orderService.updateState(2, oId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/admin/findAllOrder?page="+page+"&oState=1";
	}
	
}
